package com.netty.heartbeat;

import java.util.HashMap;
import java.util.Map;

import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class ServerHeartbeatHandler extends ChannelHandlerAdapter {
	
	private static HashMap<String,String> AUTH_IP_MAP = new HashMap<>();
	private static final String SUCCESS_KEY = "auth_success";
	
	static{
		AUTH_IP_MAP.put("192.168.30.1", "12345");
	}
	
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		//判断msg是不是字符串类型
		if(msg instanceof String){
			ahth(ctx,msg);
		}else if(msg instanceof RequestInfo){
			RequestInfo info = (RequestInfo) msg;
			System.out.println("---------------------------------");
			System.out.println("当前主机IP为："+info.getIp());
			System.out.println("当前主机CPU情况");
			Map<String,Object> cpu = info.getCpuPercMap();
			System.out.println("总使用率："+cpu.get("combined"));
			System.out.println("用户使用率："+cpu.get("user"));
			System.out.println("系统使用率："+cpu.get("sys"));
			System.out.println("等待率："+cpu.get("wait"));
			System.out.println("空闲率："+cpu.get("idle"));
			System.out.println("当前主机内存使用情况");
			Map<String,Object> memory = info.getMemoryMap();
			System.out.println("内存总量："+memory.get("total"));
			System.out.println("当前内存使用量："+memory.get("used"));
			System.out.println("当前内存剩余量："+memory.get("free"));
			System.out.println("---------------------------------");
			
			ctx.writeAndFlush("info received !");
		}else{
			ctx.writeAndFlush("connect failure !").addListener(ChannelFutureListener.CLOSE);
		}
	}

	private boolean ahth(ChannelHandlerContext ctx, Object msg) {
		String []ret = ((String)msg).split(",");
		String auth = AUTH_IP_MAP.get(ret[0]);
		if(auth != null && auth.equals(ret[1])){
			ctx.writeAndFlush(SUCCESS_KEY);
			return true;
		}else{
			ctx.writeAndFlush("auth failure !").addListener(ChannelFutureListener.CLOSE);
			return false;
		}
		
	}
}
